package com.jltx.xq.simplewatch.video.presenter.impl;

import com.jltx.xq.simplewatch.news.model.bean.TopicNews;
import com.jltx.xq.simplewatch.news.model.bean.TopicNewsEntity;
import com.jltx.xq.simplewatch.news.model.bean.TopicNewsVideoResponse;
import com.jltx.xq.simplewatch.utils.ListUtils;
import com.jltx.xq.simplewatch.video.model.impl.TopicNewsModelImpl;
import com.jltx.xq.simplewatch.video.presenter.ITopicNewsPresenter;
import com.jltx.xq.simplewatch.video.view.ITopicNewsView;

import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

import cn.jltx.utils.XqLog;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * @author jltxseo
 *         Created by junlintianxia on 2017/09/06.
 */

public class TopicNewsPresenterImpl extends ITopicNewsPresenter<ITopicNewsView>{

    private TopicNewsModelImpl topicNewsModel;

    private AtomicBoolean isLoading = new AtomicBoolean(false);

    public TopicNewsPresenterImpl(ITopicNewsView view) {
        super(view);
        topicNewsModel = new TopicNewsModelImpl();
    }

    @Override
    public void getTopicNews(String channel, boolean isDown) {
        mView.showLoadingView(isDown);
        topicNewsModel.getTopicNews(channel,isDown)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<TopicNewsEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                        isLoading.set(true);
                    }

                    @Override
                    public void onNext(TopicNewsEntity topicNewsEntity) {
                        XqLog.d(TopicNewsModelImpl.TAG, "TopicNewsPresenterImpl.onNext"+Thread.currentThread());
                        if(topicNewsEntity != null && !ListUtils.isEmpty(topicNewsEntity.new_list)){

                            for (TopicNews topicNews : topicNewsEntity.new_list){
                                XqLog.d(TopicNewsModelImpl.TAG,"---------------------");
                                XqLog.d(TopicNewsModelImpl.TAG,"title="+topicNews.getTitle());
                                XqLog.d(TopicNewsModelImpl.TAG,"url="+topicNews.getUrl());
                                XqLog.d(TopicNewsModelImpl.TAG,"---------------------");
                            }

                            if(isDown){
                                XqLog.d(TopicNewsModelImpl.TAG,"下拉数据成功.channel"+channel);
                            }else {
                                XqLog.d(TopicNewsModelImpl.TAG,"上拉数据成功.channel"+channel);
                            }

                        }else {
                            if(isDown){
                                XqLog.d(TopicNewsModelImpl.TAG,"下拉数据失败.channel"+channel);
                            }else {
                                XqLog.d(TopicNewsModelImpl.TAG,"上拉数据失败.channel"+channel);
                            }
                        }
                        mView.hideLoadingView(isDown);
                        mView.onGetNewsListSuccess((topicNewsEntity != null ? topicNewsEntity.new_list : null),(topicNewsEntity != null ? topicNewsEntity.tips : null),isDown);
                    }

                    @Override
                    public void onError(Throwable e) {
                        isLoading.set(false);
                        XqLog.d(TopicNewsModelImpl.TAG,"onError出错"+e.getMessage()+channel);
                        mView.hideLoadingView(isDown);
                        mView.showError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        isLoading.set(false);
                        XqLog.d(TopicNewsModelImpl.TAG,"onComplete"+channel);
                    }
                });
    }

    public AtomicBoolean getIsLoading() {
        return isLoading;
    }

    @Override
    public Integer getLastNewsRecordPage() {
        return topicNewsModel.getLastNewsRecordPage();
    }

    @Override
    public void getTopicNewsVideoPath(String srcUrl, final String title,final int position) {
        topicNewsModel.getTopicNewsVideoPath(srcUrl)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<TopicNewsVideoResponse>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(TopicNewsVideoResponse response) {
                        if(response.retCode == 200){ //请求视频网址成功
                            XqLog.d(TopicNewsModelImpl.TAG,"视频链接获取成功");
                            List<TopicNewsVideoResponse.DataBean.VideoBean.DownloadBean> downloadList = response.data.video.download;
                            if (!ListUtils.isEmpty(downloadList)){
                                String videoUrl = downloadList.get(downloadList.size() -1).url;//获取下载地址中最后一个地址，即超清
                                mView.onGetNewsVideoPathSuccess(videoUrl,srcUrl,title,position);
                            }else {
                                mView.onGetNewsVideoPathErro(srcUrl,title,position);
                            }
                        }else if(response.retCode == 400){
                            XqLog.d(TopicNewsModelImpl.TAG,"重新获取");
                            mView.onGetNewsVideoPathRetry(srcUrl,title,position);
                        }else {
                            mView.onGetNewsVideoPathErro(srcUrl,title,position);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.onGetNewsVideoPathErro(srcUrl,title,position);
                        XqLog.d(TopicNewsModelImpl.TAG,"onError=>"+e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        XqLog.d(TopicNewsModelImpl.TAG,"onComplete");
                    }
                });
    }
}
